#region license
# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""Wrapper for communication with external execution environments."""

namespace Grawlix.Services
import Grawlix.Meta
import System

[Disposable]
final transient class ExternalProxy[of T]:
"""
A wrapper object that manages objects in external execution environments.
Remarks: Serves to keep a reference to the environment
	while the proxy is alive.
"""
	
	def constructor(
		[required] environment as ExecutionEnvironment,
		[required] instanceType as Type,
		[required] *arguments as (object)):
	"""
	Create the cache with only an execution directory.
	Param environment: The execution environment to proxy to.
	Param instanceType: The type to instantiate and wrap (must subclass T).
	Param arguments: The arguments to pass to the instanceType constructor.
	"""
		if not instanceType.IsSubclassOf(typeof(T)) and \
			instanceType != typeof(T):
			raise ArgumentException(
				"Invalid type (${instanceType}) passed during " +
				"the creation of an ExternalProxy. " +
				"Ensure that it derives from " +
				"the generic argument (${T}).",
				"instanceType")
		_environment = environment
		_target = \
			_environment.CreateInstance(instanceType, *arguments)
	
	override def ToString():
	"""
	Get a string representation of this object.
	Returns: A string representation of this object.
	"""
		return "ExternalProxy ${_environment} ${_target}"
	
	[getter(Target)]
	private _target as T
	"""The wrapped object."""
	
	private _environment as ExecutionEnvironment
	"""The targeted execution environment."""
	
